package com.xxx.client.android.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

import com.xxx.client.android.R;
import com.xxx.client.android.CustomApplication;
import com.xxx.client.android.adapter.PurpleAdapter;
import com.xxx.client.android.adapter.PurpleEntry;
import com.xxx.client.android.adapter.SeparatedListAdapter;
import com.xxx.client.android.api.MarqueeListEntry;
import com.xxx.client.android.api.MarqueeVO;
import com.xxx.client.android.api.util.Ksoap2Caller;
import com.xxx.client.android.dialog.MarqueeListLoadingDialog;
import com.xxx.client.android.util.Tool;

public class MarqueeListActivity extends Activity {

	private MarqueeListEntry marqueeList;
	private ListView mMarqueeListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);

		mMarqueeListView = (ListView) findViewById(R.id.Listview);
	}

	@Override
	protected void onResume() {
		fillMarqueeListView();
		super.onResume();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about_menu_item:
			Tool.about(this);
			break;
		case R.id.refresh_menu_item:
			refresh();
			break;
		default:
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void refresh() {
		Ksoap2Caller.clearCache();
		new MarqueeListLoadingDialog(this, R.string.marquee_list_loading,
				R.string.marquee_list_fail).execute((Void) null);
	}

	/**
	 * Launch this Activity from the outside
	 * 
	 * @param c 
	 *            Activity from which MarqueeListActivity should be started
	 */
	public static void launch(Activity c) {
		new MarqueeListLoadingDialog(c, R.string.marquee_list_loading,
				R.string.marquee_list_fail).execute((Void) null);
	}

	/**
	 * Fills ListView with clickable menu items
	 */
	private void fillMarqueeListView() {
		CustomApplication.log("MarqueeListActivity.fillMarqueeListView");

		marqueeList = (MarqueeListEntry) getIntent().getSerializableExtra(
				"marqueeList");
		marqueeList.setHeader(getString(R.string.marquee));

		// separate adapters on one list
		SeparatedListAdapter separatedAdapter = new SeparatedListAdapter(this);
		PurpleAdapter mPurpleAdapter = new PurpleAdapter(this, false);
		ArrayList<PurpleEntry> mListEntry = new ArrayList<PurpleEntry>();
		MarqueeVO marqueeVO;
		int count = marqueeList.getCount();
		for (int i = 0; i < count; i++) {
			marqueeVO = marqueeList.getMarquee(i);
			mListEntry.add(new PurpleEntry(marqueeVO.getMarqueeContent()));
		}
		// attach list data to adapters
		mPurpleAdapter.setList(mListEntry);
		// separate adapters on one list
		separatedAdapter.addSection(marqueeList.getHeader(), mPurpleAdapter);

		mMarqueeListView.setAdapter(separatedAdapter);
	}

}
